#include <stdio.h>
#include <string.h>
#include "hi_i2c.h"
#include "typedef.h"

const unsigned int sensor_i2c_addr	=	0x60;		
const unsigned int sensor_addr_byte	=	1;
const unsigned int sensor_data_byte	=	1;

BOOL i2c_read_byte(BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device)
{
    int fd = -1;
    int ret;
    I2C_DATA_S i2c_data;

    fd = open("/dev/hi_i2c", 0);
    if(fd<0)
    {
        printf("Open hi_i2c error!\n");
        return -1;
    }

    i2c_data.dev_addr = address;
    i2c_data.reg_addr = offset;
    i2c_data.addr_byte_num = 1;
    i2c_data.data_byte_num = byteno;

    ret = ioctl(fd, CMD_I2C_READ, &i2c_data);

    if (ret)
    {
        printf("hi_i2c read faild!\n");
        close(fd);
        return -1;
    }

    close(fd);
    *p_data = i2c_data.data;
    return i2c_data.data;
}

BOOL i2c_write_byte( BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device )
{
    int fd = -1;
    int ret;
    I2C_DATA_S i2c_data;

    fd = open("/dev/hi_i2c", 0);
    if(fd<0)
    {
        printf("Open hi_i2c error!\n");
        return -1;
    }

    i2c_data.dev_addr = address;
    i2c_data.reg_addr = offset;
    i2c_data.addr_byte_num =1;
    i2c_data.data = *p_data;
    i2c_data.data_byte_num = byteno;

    ret = ioctl(fd, CMD_I2C_WRITE, &i2c_data);

    if (ret)
    {
        printf("hi_i2c write faild!\n");
        close(fd);
        return -1;
    }

    close(fd);
    return 0;
}
